/**
 * MCP服务
 * 处理MCP（Model Context Protocol）相关的功能
 */
import { BaseService } from './base-service'
import type { ApiResponse } from '../../../types/api'

// MCP信息
export interface MCP {
  id: string
  name: string
  description?: string
  enabled: boolean
  serverName?: string
  createdAt?: string
  updatedAt?: string
}

// MCP工具信息
export interface MCPTool {
  name: string
  description?: string
  parameters?: any
}

// MCP分页查询参数
export interface MCPPageParams {
  searchName?: string
  page: number
  pageSize: number
}

// MCP状态更新参数
export interface MCPStatusParams {
  id: string
  enabled: boolean
}

export class MCPService extends BaseService {
  constructor() {
    super()
  }

  /**
   * 获取MCP分页列表
   */
  async getMcpPage(params: MCPPageParams): Promise<ApiResponse<any>> {
    const response = await this.get('/mcp/page', params)
    
    // 统一响应格式处理
    if (response.code === 0) {
      return {
        ...response,
        data: response.data || {}
      }
    }
    
    return response
  }

  /**
   * 添加MCP
   */
  async addMcp(mcp: Omit<MCP, 'id' | 'createdAt' | 'updatedAt'>): Promise<ApiResponse<any>> {
    const response = await this.post('/mcp/entity/save', mcp)
    
    // 统一响应格式处理
    if (response.code === 0) {
      return {
        ...response,
        data: response.data || {}
      }
    }
    
    return response
  }

  /**
   * 更新MCP
   */
  async updateMcp(id: string, mcp: Partial<MCP>): Promise<ApiResponse<any>> {
    const response = await this.put(`/mcp/entity/${id}`, mcp)
    
    // 统一响应格式处理
    if (response.code === 0) {
      return {
        ...response,
        data: response.data || {}
      }
    }
    
    return response
  }

  /**
   * 更新MCP启用状态
   */
  async updateMcpEnableStatus(params: MCPStatusParams): Promise<ApiResponse<any>> {
    const response = await this.put('/mcp/enable', params)
    
    // 统一响应格式处理
    if (response.code === 0) {
      return {
        ...response,
        data: response.data || {}
      }
    }
    
    return response
  }

  /**
   * 删除MCP
   */
  async deleteMcp(id: string): Promise<ApiResponse<any>> {
    const response = await this.delete(`/mcp/entity/${id}`)
    
    // 统一响应格式处理
    if (response.code === 0) {
      return {
        ...response,
        data: response.data || {}
      }
    }
    
    return response
  }

  /**
   * 根据MCP名称获取工具
   */
  async getMcpToolsByName(mcpName: string): Promise<ApiResponse<MCPTool[]>> {
    const response = await this.get('/mcp/toolsByName', { mcpName })
    
    // 统一响应格式处理
    if (response.code === 0) {
      return {
        ...response,
        data: response.data || []
      }
    }
    
    return response
  }

  /**
   * 重连MCP服务
   */
  async reconnectMcp(serverName?: string): Promise<ApiResponse<any>> {
    const response = await this.post('/mcp/reconnect', null, {
      params: serverName ? { serverName } : undefined
    })
    
    // 统一响应格式处理
    if (response.code === 0) {
      return {
        ...response,
        data: response.data || {}
      }
    }
    
    return response
  }

  /**
   * 刷新MCP工具列表
   */
  async refreshMcpTools(): Promise<ApiResponse<any>> {
    const response = await this.post('/mcp/refresh')
    
    // 统一响应格式处理
    if (response.code === 0) {
      return {
        ...response,
        data: response.data || {}
      }
    }
    
    return response
  }
}

// 创建单例实例
export const mcpService = new MCPService()

export default MCPService